package cc.android.testapp.ui.applist;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import java.util.List;

import cc.android.testapp.R;
import cc.android.testapp.ui.binding.ActivityAppListBinding;
import cc.android.testapp.util.CLog;

public class AppListActivity extends AppCompatActivity {

    private ActivityAppListBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityAppListBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        PackageManager tPM = this.getPackageManager();
        List<ApplicationInfo> tApps = tPM.getInstalledApplications(0);
        new Thread(() -> {
            CLog.log("获取了应用数量为: " + tApps.size());
            AppLiseView.AppInfo[] tInfos = new AppLiseView.AppInfo[20];
            for (int i = 0; i < 20; i++) {
                tInfos[i] = new AppLiseView.AppInfo(tPM, tApps.get(i));
            }
            // 排序比较耗时,也放线程里
            AppLiseView tView=new AppLiseView(tInfos, R.layout.layout_app_line);

            new Handler(Looper.getMainLooper()).post(() -> {
                binding.tvApplistLoading.setVisibility(View.GONE);
                binding.llAppList.setLayoutManager(new LinearLayoutManager(this));
                binding.llAppList.setAdapter(tView);
                binding.llAppList.setVisibility(View.VISIBLE);
            });
        }).start();

        setSupportActionBar(binding.toolbar);
    }

    @Nullable
    @Override
    public ActionMode startSupportActionMode(@NonNull ActionMode.Callback callback) {
        return super.startSupportActionMode(callback);
    }
}